package com.basker.pisces.core.meta;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.basker.pisces.core.IDataContract;
import com.basker.pisces.core.IDataContractCollection;

public class CollectionField extends AbstractField implements ICollectionField {

    private IDataObjectMeta itemObjectMeta;
    private String fkFieldDescription;

    public CollectionField(String name, String displayName) {
        this(name, displayName, null);
    }

    public CollectionField(String name, String displayName, IDataObjectMeta itemObjectMeta) {
        super(name, displayName);
        this.itemObjectMeta = itemObjectMeta;
    }

    @Override
    public String getFkFieldDescription() {
        return fkFieldDescription;
    }

    @Override
    public IDataObjectMeta getItemObjectMeta() {
        return itemObjectMeta;
    }

    @Override
    public List<IDataContract> getRemovedObjects(Object parentDataObject) {
        List<IDataContract> ret = null;

        Object value = this.getValue(parentDataObject);
        if (value instanceof IDataContractCollection) {
            ret = ((IDataContractCollection) value).getRemovedObjects();
        }

        return ret != null ? ret : Collections.emptyList();

    }

    @Override
    public Collection<Object> getValue(Object dataObject) {
        @SuppressWarnings("unchecked")
        Collection<Object> col = (Collection<Object>) super.getValue(dataObject);
        return col;
    }

    public void setFkFieldDescription(String fkFieldDescription) {
        this.fkFieldDescription = fkFieldDescription;
    }

    public void setItemObjectMeta(IDataObjectMeta itemObjectMeta) {
        this.itemObjectMeta = itemObjectMeta;
    }

}
